package com.meiwenhui.callu;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

import com.meiwenhui.callu.broadcast.AlarmReceiver;

public class AddContactActivity extends Activity {
	private static final String TAG = "CalluActivity";
	Button mButtonStart;
	Button mButtonStop;
	EditText telPhoneEditText;

	TextView mTextView;
	TelephonyManager mTelephonyMgr;
	Calendar calendar;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.add_contact);
		calendar = Calendar.getInstance();

		mTextView = (TextView) findViewById(R.id.phone_view);
		mButtonStart = (Button) findViewById(R.id.set_time);
		mButtonStop = (Button) findViewById(R.id.cancel_time);
		telPhoneEditText = (EditText) findViewById(R.id.tel_number);
		mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

		mTelephonyMgr.listen(new PhoneStateListener() {
			public void onCallStateChanged(int state, String incomingNumber) {
				super.onCallStateChanged(state, incomingNumber);
				switch (state) {
				case TelephonyManager.CALL_STATE_IDLE: {
					Log.e(TAG, "CALL_STATE_IDLE");
					break;
				}
				case TelephonyManager.CALL_STATE_OFFHOOK: {
					Log.e(TAG, "CALL_STATE_OFFHOOK");
					break;
				}
				case TelephonyManager.CALL_STATE_RINGING: {
					Log.e(TAG, "CALL_STATE_RINGING");
					break;
				}
				default:
					break;
				}
			}
		}, PhoneStateListener.LISTEN_CALL_STATE);

		mButtonStart.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				calendar.setTimeInMillis(System.currentTimeMillis());
				int mHour = calendar.get(Calendar.HOUR_OF_DAY);
				int mMinute = calendar.get(Calendar.MINUTE);
				new TimePickerDialog(AddContactActivity.this, new TimePickerDialog.OnTimeSetListener() {
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						calendar.setTimeInMillis(System.currentTimeMillis());
						calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
						calendar.set(Calendar.MINUTE, minute - 1);
						calendar.set(Calendar.SECOND, 0);
						calendar.set(Calendar.MILLISECOND, 0);

						Intent intent = new Intent(AddContactActivity.this, AlarmReceiver.class);
						intent.putExtra("tel_phone", telPhoneEditText.getText().toString());
						PendingIntent pendingIntent = PendingIntent.getBroadcast(AddContactActivity.this, 0, intent, 0);
						AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
						am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), (24 * 60 * 60 * 1000), pendingIntent);
						SharedPreferences preference = getSharedPreferences("zhuyan", Context.MODE_PRIVATE);
						Editor edit = preference.edit();
						edit.putString("tel_phone", telPhoneEditText.getText().toString());
						edit.commit();
						String tmpS = telPhoneEditText.getText().toString() + "->" + format(hourOfDay) + ":" + format(minute);
						mTextView.setText(tmpS);
					}
				}, mHour, mMinute, true).show();
			}
		});

		mButtonStop.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(AddContactActivity.this, AlarmReceiver.class);
				PendingIntent pendingIntent = PendingIntent.getBroadcast(AddContactActivity.this, 0, intent, 0);
				AlarmManager am;
				am = (AlarmManager) getSystemService(ALARM_SERVICE);
				am.cancel(pendingIntent);
				mTextView.setText("Cancelled");
			}
		});
	}

	private String format(int x) {
		String s = "" + x;
		if (s.length() == 1)
			s = "0" + s;
		return s;
	}

	@Override
	protected void onStart() {
		super.onStart();
//		SharedPreferences preference = getSharedPreferences("zhuyan", Context.MODE_PRIVATE);
//		telPhoneEditText.setText(preference.getString("tel_phone", "10086"));
	}

	@Override
	protected void onStop() {
		super.onStop();
		SharedPreferences preference = getSharedPreferences("zhuyan", Context.MODE_PRIVATE);
		Editor edit = preference.edit();
		edit.putString("tel_phone", telPhoneEditText.getText().toString());
		edit.commit();
	}
}